package com.shop.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.entity.SupplierGoods;
import com.shop.mapper.SupplierGoodsMapper;
import com.shop.model.PageParam;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * (SupplierGoods)表服务接口
 *
 * @author makejava
 * @since 2020-11-21 10:12:56
 */
@Service
@AllArgsConstructor
public class SupplierGoodsService {
    private final SupplierGoodsMapper supplierGoodsMapper;

    /**
     * 查询商品
     */
    public PageInfo<SupplierGoods> getGoodsList(PageParam<SupplierGoods> pageParam, Integer supplierId) {
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
//            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
//                PageHelper.orderBy(pageParam.getOrderParams()[i]);
//            }
            return new PageInfo<SupplierGoods>(supplierGoodsMapper.superSearch(pageParam.getSuperSearchKeyWord()));
        } else {
            //查询列表
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
//        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
//            PageHelper.orderBy(pageParam.getOrderParams()[i]);
//        }
            List<SupplierGoods> supplierGoodsList=supplierGoodsMapper.getGoodsList(pageParam.getModel().getCategoryId(),supplierId);
            for (SupplierGoods supplierGoods : supplierGoodsList) {
                System.out.println(supplierGoods);
            }
            return new PageInfo<SupplierGoods>(supplierGoodsList);

        }
    }

    /**
     * 查询商品
     */
    public PageInfo<SupplierGoods> showGoodsList(PageParam<SupplierGoods> pageParam) {
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
//            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
//                PageHelper.orderBy(pageParam.getOrderParams()[i]);
//            }
            return new PageInfo<SupplierGoods>(supplierGoodsMapper.superSearch(pageParam.getSuperSearchKeyWord()));
        } else {
            //查询列表
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
//        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
//            PageHelper.orderBy(pageParam.getOrderParams()[i]);
//        }
            List<SupplierGoods> supplierGoodsList=supplierGoodsMapper.showGoodsList((SupplierGoods)pageParam.getModel());
            for (SupplierGoods supplierGoods : supplierGoodsList) {
                System.out.println(supplierGoods);
            }
            return new PageInfo<SupplierGoods>(supplierGoodsList);

        }
    }

    /**
     * 修改商品
     * @return
     */
    public int updateGoodsName(SupplierGoods supplierGoods) {
        return supplierGoodsMapper.updateByGoodsId(supplierGoods);
    }

    /**
     * 新增商品
     * @return
     */
    public int insertGoods(SupplierGoods supplierGoods) {
        return supplierGoodsMapper.insertGoods(supplierGoods);
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    public int delGoods(Integer id) {
        return supplierGoodsMapper.deleteById(id);
    }

    /**
     * 通过商品类别id查询该商户下所有商品
     * @param categoryId
     * @return
     */
    public List<SupplierGoods> getGoodsListByCategoryId(String categoryId) {
        return supplierGoodsMapper.getGoodsListByCategoryId(Integer.parseInt(categoryId));
    }
}
